package com.lmx.repoDomain.repository;

import io.katharsis.queryParams.QueryParams;
import io.katharsis.repository.annotations.JsonApiFindAll;
import io.katharsis.repository.annotations.JsonApiResourceRepository;
import io.katharsis.repository.annotations.JsonApiSave;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.lmx.common.constants.CommonConstants;
import com.lmx.common.context.ActionContextHolder;
import com.lmx.common.util.CommonUtil;
import com.lmx.persistence.service.DeviceService;
import com.lmx.repoDomain.exception.BusinessException;
import com.lmx.repoDomain.tmodel.Camera;

/**
 * 食物
 *
 * @author ytche
 * @date 2017年8月30日
 *
 */
@JsonApiResourceRepository(value = Camera.class)
@Component
public class CameraRepository extends BaseRepository<Camera> {

	@Autowired
	private DeviceService deviceService;

	@PostConstruct
	public void init() {
		super.dslJpaService.setEntityClass(Camera.class);
	}

	@Override
	@JsonApiFindAll
	public Iterable<Camera> findAll(final QueryParams requestParams) {
		Map<String, String> keyMap = CommonUtil.getKeyMapByFilter(requestParams);
		List<Camera> dataList = new ArrayList<>();
		String queryData = "";
		String queryType = "";
		Short deviceStatus = -1;// 1 可用 0 不可用
		if (keyMap.get("queryType") != null) {// 查询类型
			queryType = keyMap.get("queryType");
		}
		if (keyMap.get("queryData") != null) {// 查询条件
			queryData = keyMap.get("queryData");
		}
		if (keyMap.get("deviceStatus") != null && keyMap.get("deviceStatus").matches("\\d+")) {// 查询条件
			deviceStatus = Short.valueOf(keyMap.get("deviceStatus"));
		}

		if ("queryAll".equals(queryType)) {
			// 搜索device from Room bed
			dataList = deviceService.geCameraFromAll(queryData, deviceStatus);
			commonService.setPageInfo(dataList, requestParams);
			dataList = commonService.getPageData(dataList, requestParams);
		} else {
			dataList = (List) super.findAll(requestParams);
		}
		if (dataList != null) {
			for (Camera data : dataList) {
				// 设置观看人数
				if (data.getCode() != null && ActionContextHolder.getCameraTokenHolder().containsKey(data.getCode())
						&& ActionContextHolder.getCameraTokenHolder().get(data.getCode()) != null) {
					data.setAudienceNum(ActionContextHolder.getCameraTokenHolder().get(data.getCode()).size());
				} else {
					data.setAudienceNum(0);
				}
			}
		}
		return dataList;
	}

	@Override
	@JsonApiSave
	public Camera save(Camera model) {
		if (model.getId() == null) {
			Camera temp = deviceService.getCameraByCode(model);
			if (temp == null) {
				if (model.getStatus() == null) {
					model.setStatus(0);
				}
				if (model.getLiveStatus() == null) {
					model.setLiveStatus(0);
				}
				model = super.save(model);
			} else {
				model.setErrcode(4);// code重复
				throw new BusinessException(null, CommonConstants.ERROR_CODE.NAME_ERROR.label(), null);
			}
		} else {
			if (model.getDelStatus() == 1) {// 删除
				// 删除 验证 CustomerFoodPlan errorcode 8
				dslJpaService.update(model);
			} else {
				if (model.getOperateFlag() != null && ((CommonConstants.DEVICE_OPERATE_CODE.BIND.label()
						.equals(model.getOperateFlag())
						|| (CommonConstants.DEVICE_OPERATE_CODE.REMOVE_BIND.label().equals(model.getOperateFlag()))))) {// 进行解绑或者绑定操作
					deviceService.operateDeviceByFlag(null, model);
				} else {// 修改device信息
					dslJpaService.update(model);
				}
			}
		}
		return model;
	}

}
